package com.xszx.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.xzsx.entity.Student;

public class Ajax extends HttpServlet {


	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		//接收前端信息
		System.out.println("进入ajax");
		resp.setContentType("text/json");
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		//接收参数
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		System.out.println(name+"\t"+age);
		
		//返回数据
		PrintWriter pw = resp.getWriter();
		//查询所有学生信息
		Student s1 = new Student("1001", "张三", "1801");
		Student s2 = new Student("1002", "李四", "1801");
		Student s3 = new Student("1003", "王五", "1801");
		List<Student> list = new ArrayList<Student>();
		list.add(s1);
		list.add(s2);
		list.add(s3);
		//将list信息转成JSON串
		String json = JSON.toJSONString(list);
		pw.println(json);
	}
	

}
